/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.edmo.fastener.slave;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author obelog
 */
public class GenerateSuppliers {
    private ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
    private String[] material = new String[]{"Metal", "Plastic"};
    private IocBean dir = (IocBean) context.getBean("folder");
    
    public static void main(String[] args){
        new GenerateSuppliers().writeData();
    }

    public void writeData() {
        try {
            IocBean ioc = (IocBean) context.getBean("suppliers");
            File file = new File(ioc.getName());
            new File(dir.getName()+"\\SUPPLIERS").mkdirs();

            if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            generator(bw);
            bw.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    private void generator(BufferedWriter bw) throws IOException{
        for (int i=1; i<2001; i++){
            int count = (int)(Math.random()*3 + 1);
            int state = (int)(Math.random() * 51 + 1);
            int delivery = (int)(Math.random()*10 + 1);
            int discount = (int)(Math.random()*20);
            new File(dir.getName()+"\\SUPPLIERS\\" + "S_00" + i).mkdirs();
            for (int k=0; k<count; k++){
                int mat = (int)(Math.random()*2);
                int rang1 = (int)(Math.random()*900000);
                int rang2 = (int)(Math.random()* (999999 - rang1) + rang1);
               String content = "S_00" + i + "|State" + state + "|" + material[mat] + "|" + rang1 + "-" + rang2 + "|" + delivery + "|" + discount +"\n"; 
               bw.write(content);
            }
        }
    }
}
